package com.zds.scm.dao.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.DeptInfoDAO;
import com.zds.scm.domain.DeptInfo;

public class DeptInfoDAOImpl extends AbstractDAOImpl<DeptInfo> implements DeptInfoDAO {

	@Override
	protected DeptInfo rowMapper(ResultSet rs) throws SQLException {
		DeptInfo deptInfo=new DeptInfo();
		try {
			deptInfo.setDeptCode(rs.getString("dept_code"));
			deptInfo.setDeptName(rs.getString("dept_name"));
			deptInfo.setDeptDesc(rs.getString("dept_desc"));
		} catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
			   logger.error("DAO层结果集映射处理错误:"+e.getMessage());
			   throw new DaoException(e.getMessage(),e);
		}
		return deptInfo;
	}
	@Override
	public boolean delete(DeptInfo deptInfo) {
		String sql="delete from deptinfo where dept_code=?";
		Object[] args=new Object[]{deptInfo.getDeptCode()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean insert(DeptInfo deptinfo) {
		String sql="insert into deptinfo(dept_code,dept_name,dept_desc) values(?,?,?)";
		Object[] args=new Object[]{deptinfo.getDeptCode(),deptinfo.getDeptName(),deptinfo.getDeptDesc()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean update(DeptInfo deptinfo) {
		String sql="update deptinfo set dept_name=?,dept_desc=? where dept_code=?";
		Object[] args=new Object[]{deptinfo.getDeptName(),deptinfo.getDeptDesc(),deptinfo.getDeptCode()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
	public List<DeptInfo> findAll() {
		return findAll(-1,-1);
	}
	@Override                         
	public List<DeptInfo> findAll(int currentPage, int pageSize){
		String sql="select * from deptinfo order by dept_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{};
		}else {
			sql+=" limit ?,?";     
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
    @Override
	public int getTotalRows(){
		String sql="select count(*) from deptinfo";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	@Override
	public DeptInfo findById(Serializable id) {
		String sql="select * from deptinfo where dept_code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}
	public List<DeptInfo> findByNameAndCode(String likeDeptName,String likeDeptCode,int currentPage, int pageSize){
		String sql="select * from deptinfo where dept_name like ? and dept_code like ? order by dept_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeDeptName+"%","%"+likeDeptCode+"%"};
		}else {
			sql+=" limit ?,?";     
			args=new Object[]{"%"+likeDeptName+"%","%"+likeDeptCode+"%",(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	   
	public int getTotalRowsByNameAndCode(String likeDeptName,String likeDeptCode){
		String sql="select count(*) from deptinfo where dept_name like ? and dept_code like ?";
		Object[] args=new Object[]{"%"+likeDeptName+"%","%"+likeDeptCode+"%"};
		return super.findEntityCount(sql, args);
	}
}
